package com.jie.springwebapp.controller;

import com.jie.springwebapp.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

// 请求处理类
//@RestController
public class HelloController {
//    @RequestMapping("/hello")
//    public String hello(){
//        System.out.println("hello");
//        return "hello";
//    }
//    @RequestMapping("/simpleParam")
//    public String simpleParam(HttpServletRequest request) {
//        String name = request.getParameter("name");
//        String ageStr = request.getParameter("age");
//        int age = Integer.parseInt(ageStr);
//        System.out.println(name+":"+age);
//        return "OK";
//    }
//    @PostMapping("/simpleParam") // Post请求
//    public String simpleParam(@RequestParam(value = "name",required = false) String usrname, Integer age) {
//        System.out.println(usrname+" "+age);
//        return "OK";
//    }
//    @GetMapping("/simplePojo")
//    public String simplePojo(User user) {
//        System.out.println(user);
//        return "OK";
//    }
//    @RequestMapping("/complexPojo")
//    public String complexPojo(User user) {
//        System.out.println(user);
//        return "OK";
//    }
//    @RequestMapping("/arrayParam")
//    public String arrayParam(String[] hobby) {
//        System.out.println(Arrays.toString(hobby));
//        return "OK";
//    }
//    @RequestMapping("/listParam")
//    public String listParam(@RequestParam List<String> hobby) {
//        System.out.println(hobby);
//        return "OK";
//    }
//    @RequestMapping("/dateParam")
//    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime) {
//        System.out.println(updateTime);
//        return "OK";
//    }
//    @PostMapping("/jsonParam")
//    public String jsonParam(@RequestBody User user) {
//        System.out.println(user);
//        return "OK";
//    }
//    @RequestMapping("/path/{id}/{name}")
//    public String pathParam(@PathVariable Integer id,@PathVariable String name){
//        System.out.println(id+" "+name); // id 需要和path{id}保持一致
//        return "OK";
//    }

    @RequestMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return "OK";
    }
//    @RequestMapping("/simpleParam")
//    public String simpleParam(HttpServletRequest request){
//        System.out.println(request.getParameter("name")+" "+request.getParameter("age"));
//        return "OK";
//    }
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestParam(required = true,value="name") String name,@RequestParam(required = false,value="age") Integer age) {
        System.out.println(name+" "+age);
        return "OK";
    }
    @RequestMapping("/simplePojo")
    public String simplePojo(User user) {
        System.out.println(user);
        return "OK";
    }
    @RequestMapping("/jsonPojo")
    public String jsonPojo(@RequestBody User user) {
        System.out.println(user);
        return "OK";
    }
    @RequestMapping("/path{id}/{name}")
    public String pathParam(@PathVariable(value = "id") Integer id,@PathVariable(value = "name") String name) {
        System.out.println(name+" "+id);
        return "OK";
    }
}
